﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.08.04
 * 描 述：流程（我的任务）	
 */
var refreshGirdData;
var tabindex = request("categoryId");

var bootstrap = function ($, learun) {
    "use strict";
    var categoryId = '1';
    var departmentId = '';
    var schemeCode = '';
    var status;
    var logbegin = '';
    var logend = '';

    refreshGirdData = function () {
        page.search();
    }

    var page = {
        init: function () {
            $('#lr_verify').hide();
            page.initleft();
            page.initGrid();
            page.bind();
        },
        bind: function () {
            $('#datesearch').lrdate({
                dfdata: [
                    {
                        name: '今天', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00')
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近7天', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'd', -6)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近1个月', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'm', -1)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近3个月', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'm', -3)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                ],
                // 月
                mShow: false,
                premShow: false,
                // 季度
                jShow: false,
                prejShow: false,
                // 年
                ysShow: false,
                yxShow: false,
                preyShow: false,
                yShow: false,
                // 默认
                dfvalue: '2',
                selectfn: function (begin, end) {
                    logbegin = begin;
                    logend = end;

                    page.search();
                }
            });

            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 部门选择
            $('#departmentId').lrselect({
                type: 'default',
                placeholder: '部门',
                maxHeight: 280,
                height: 280,
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (item && item.value != '') {
                        departmentId = item.value;
                    } else {
                        departmentId = '';
                    }
                }
            });
            $('#departmentId').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'departments/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {companyId: null, parentId: '0'},
            });
            // 流程
            $('#schemeCode').lrselect({
                type: 'default',
                placeholder: '流程名',
                value: "code",
                text: "name",
                title: "name",
                maxHeight: 280,
                height: 280,
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (item && item.code != '') {
                        schemeCode = item.code;
                    } else {
                        schemeCode = '';
                    }
                }
            });
            $('#schemeCode').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'wfschemes/list?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {},
            });
            // 状态
            $('#status').lrselect({
                placeholder: '处理状态',
                select: function (item) {
                    if (item && item.value != '') {
                        status = item.value;
                    } else {
                        status = null;
                    }
                }
            });

            // 查看流程进度
            $('#lr_eye').on('click', function () {
                    var processId = $('#gridtable').jfGridValue('id');
                    var taskId = $('#gridtable').jfGridValue('taskId');
                    var processName = $('#gridtable').jfGridValue('code') + $('#gridtable').jfGridValue('processName');
                    var taskName = $('#gridtable').jfGridValue('taskName');

                    if (learun.checkrow(processId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: processId + taskId,
                                css: 'fa magic',
                                name: '查看流程进度【' + processName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + taskId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: processId + taskId,
                                title: '查看流程进度【' + processName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + taskId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                }
            );
            // 发起流程
            $('#lr_release').on('click', function () {
                learun.layerForm({
                    id: 'form',
                    title: '选择流程模板',
                    url: config.webportal_server + 'pages/workflow/wfmyTask/releaseForm.html?access_token=' + config.getToken().access_token,
                    height: 600,
                    width: 825,
                    maxmin: true,
                    callBack: function (id) {
                        return top[id].acceptClick();
                    }
                });
            });
            // 审核流程
            $('#lr_verify').on('click', function () {
                var processId = $('#gridtable').jfGridValue('id');
                var taskId = $('#gridtable').jfGridValue('taskId');
                var processName = $('#gridtable').jfGridValue('processName');
                var taskName = $('#gridtable').jfGridValue('taskName');
                var taskType = $('#gridtable').jfGridValue('taskType');
                if (taskType == 4) {
                    if (learun.checkrow(taskId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: taskId,
                                css: 'fa magic',
                                name: '审核流程【' + processName + '/' + taskName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=4' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: taskId,
                                title: '审核流程【' + processName + '/' + taskName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=4' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                } else if (taskType == 1) {
                    if (learun.checkrow(taskId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: taskId,
                                css: 'fa magic',
                                name: '审核流程【' + processName + '/' + taskName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=1' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: taskId,
                                title: '审核流程【' + processName + '/' + taskName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=1' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                } else if (taskType == 2) {
                    learun.alert.warning('请点击重新发起');
                } else {
                    if (learun.checkrow(taskId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: taskId,
                                css: 'fa magic',
                                name: '审核流程【' + processName + '/' + taskName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=3' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: taskId,
                                title: '审核流程【' + processName + '/' + taskName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + taskId + '&type=3' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                }
            });
        },
        initleft: function () {
            $('#lr_left_list li').on('click', function () {
                var $this = $(this);
                var $parent = $this.parent();
                $parent.find('.active').removeClass('active');
                $this.addClass('active');

                categoryId = $this.attr('data-value');

                if (categoryId == '2') {
                    $('#lr_verify').show();
                } else {
                    $('#lr_verify').hide();
                }

                page.search();
            });

            if (!!tabindex) {
                categoryId = tabindex;
                $("#lr_left_list li[data-value='" + tabindex + "']").addClass('active');
                $("#lr_left_list li[data-value!='" + tabindex + "']").removeClass('active');
                if (categoryId == '2') {
                    $('#lr_verify').show();
                } else {
                    $('#lr_verify').hide();
                }

                page.search();
            }
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'wftasks_all?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "流程单号", name: "code", width: 120, align: "center"},
                    {label: "标题", name: "processName", width: 220, align: "left"},
                    {label: "模板名称", name: "schemeName", width: 150, align: "left"},

                    {
                        label: "等级", name: "processLevel", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            switch (cellvalue) {
                                case 0:
                                    return '普通';
                                    break;
                                case 1:
                                    return '重要';
                                    break;
                                case 2:
                                    return '紧急';
                                    break;
                            }
                        }
                    },
                    {
                        label: "状态", name: "status", width: 70, align: "center",
                        formatter: function (cellvalue, row) {
                            if (row.isFinished == 0) {
                                if (cellvalue == 1) {
                                    return "<span class=\"label label-success\">运行中</span>";
                                } else if (cellvalue == -1) {
                                    return "<span class=\"label label-default\">草稿</span>";
                                }
                            } else {
                                return "<span class=\"label label-warning\">结束</span>";
                            }

                        }
                    },
                    {label: "部门", name: "departmentName", width: 100, align: "center"},
                    {label: "发起者", name: "createUsername", width: 70, align: "center"},
                    {
                        label: "发起时间", name: "createTime", width: 150, align: "left",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "备注", name: "description", width: 300, align: "left"}

                ],
                dblclick: function (row) {
                    var processId = row.id;
                    var taskId = row.taskId;
                    var processName = row.code + " " + row.processName;
                    if (learun.checkrow(processId)) {
                        if (config.isTabs()) { //主题1
                            learun.frameTab.open({
                                id: processId + taskId,
                                css: 'fa magic',
                                name: '查看流程进度【' + processName + '】',
                                path: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + taskId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                            });
                        } else {
                            learun.layerForm({
                                id: processId,
                                title: '查看流程进度【' + processName + '】',
                                url: config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + taskId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                height: 1080,
                                width: 1920
                            });
                        }
                    }
                },
                mainId: 'id',
                isPage: true,
                sidx: 'createTime'
            });
        },
        search: function (param) {
            param = param || {};
            param.startTime = logbegin;
            param.endTime = logend;
            param.categoryId = categoryId;
            param.schemeCode = schemeCode;
            param.departmentId = departmentId;
            param.status = status;

            $('#gridtable').jfGridSet('reload', param);
        }
    };

    page.init();
}


